package com.hc.acoustics.ui.activity

import android.content.Intent
import android.os.Bundle
import com.hc.acoustics.R
import com.hc.acoustics.base.BaseActivity
import com.hc.acoustics.util.FormulaUtil
import com.yc.yclibrary.base.YcAppCompatActivity
import kotlinx.android.synthetic.main.formula_button.*
import kotlinx.android.synthetic.main.type2_formula4_location_activity.radioButton
import kotlinx.android.synthetic.main.type2_formula4_location_activity.radioButton2
import kotlinx.android.synthetic.main.type2_formula5_location_activity.*
import java.lang.NumberFormatException

/**
 *  声学 - 声压级相减
 */
class Type2Formula5Activity : BaseActivity() {

    companion object {
        private const val KEY_TITLE = "key_title"
        @JvmStatic
        fun newInstance(activity: YcAppCompatActivity, title: String) {
            val intent = Intent(activity, Type2Formula5Activity::class.java)
            intent.putExtra(KEY_TITLE, title)
            activity.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.type2_formula5_location_activity
    }

    override fun initView(savedInstanceState: Bundle?) {
        setActionBar(intent.getStringExtra(KEY_TITLE))
        formulaResetBtn.setOnClickListener {
            formula2_5_s1_TEV.reset()
            formula2_5_s2_TEV.reset()
            formula2_5_spl2_TEV.reset()
            formula2_5_spl3_TEV.reset()
            formula2_5_spl4_TEV.reset()
            formula2_5_spl5_TEV.reset()
            formula2_5_spl6_TEV.reset()
            formula2_5_spl7_TEV.reset()
            formula2_5_spl8_TEV.reset()
            formula2_5_result_TEV.reset()
        }
        formulaOkBtn.setOnClickListener {
            try {
                var result: Double?
                if (radioButton.isSelect()) {
                    result = FormulaUtil.formula2_5_1(
                        formula2_5_s1_TEV.getDouble(),
                        formula2_5_s2_TEV.getDouble(),
                        formula2_5_spl2_TEV.getDouble(),
                        formula2_5_spl3_TEV.getDouble(),
                        formula2_5_spl4_TEV.getDouble(),
                        formula2_5_spl5_TEV.getDouble(),
                        formula2_5_spl6_TEV.getDouble(),
                        formula2_5_spl7_TEV.getDouble(),
                        formula2_5_spl8_TEV.getDouble()
                    )
                } else {
                    result = FormulaUtil.formula2_5_2(
                        formula2_5_s1_TEV.getDouble(),
                        formula2_5_s2_TEV.getDouble(),
                        formula2_5_spl2_TEV.getDouble(),
                        formula2_5_spl3_TEV.getDouble(),
                        formula2_5_spl4_TEV.getDouble(),
                        formula2_5_spl5_TEV.getDouble(),
                        formula2_5_spl6_TEV.getDouble(),
                        formula2_5_spl7_TEV.getDouble(),
                        formula2_5_spl8_TEV.getDouble()
                    )
                }
                formula2_5_result_TEV.setText(result)
            } catch (e: NumberFormatException) {
                formula2_5_result_TEV.setText("Infinity")
            }
        }
        radioButton.setOnClickListener {
            radioButton.setSelect(!radioButton.isSelect())
            radioButton2.setSelect(!radioButton2.isSelect())
        }
        radioButton2.setOnClickListener {
            radioButton.setSelect(!radioButton.isSelect())
            radioButton2.setSelect(!radioButton2.isSelect())
        }
    }
}